package com.weifeng.learneasyexcel.learnpoi;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class Write {
    public static void main(String[] args) throws IOException {
       String file07 = "/Users/wumingyang/Desktop/JAVA项目/http-client-demo/excel/07版.xlsx";

//       String file03 = "C:\\Users\\Administrator\\Desktop\\03版.xls";
//       write03(file03);
//       write03bigdata(file03);

//        String file07 = "C:\\Users\\Administrator\\Desktop\\07版.xlsx";
        write07(file07);
    }

    public static void write03( String file) throws IOException{

        //创建excel HSSFWorkbook 是03版本,XSSFWorkbook 是07版  SXSSFWorkbook 是07的优化版
        Workbook sheets = new HSSFWorkbook();
        //创建第一个sheet
        Sheet sheet1 = sheets.createSheet("这是第一个sheet");
        //第一行 从0 开始算
        Row row = sheet1.createRow(0);
        //第一行的 第一列
        Cell cell = row.createCell(0);
        cell.setCellValue("name");

        Cell cell1 = row.createCell(1);
        cell1.setCellValue("age");

        row.createCell(2).setCellValue("height");


        Row row1 = sheet1.createRow(1);

        row1.createCell(0).setCellValue("weifeng");
        row1.createCell(1).setCellValue("20");
        row1.createCell(2).setCellValue(80);

//        FileOutputStream fileOutputStream = new FileOutputStream("/Users/wumingyang/Desktop/JAVA项目/http-client-demo/excel/03版.xls");
        FileOutputStream fileOutputStream = new FileOutputStream(file);


        sheets.write(fileOutputStream);
        fileOutputStream.close();

    }
//输出 03版excel 最大条数65535
    public static void write03bigdata(String file) throws IOException{
        long l1 = System.currentTimeMillis();
        HSSFWorkbook sheets = new HSSFWorkbook();
        HSSFSheet sheet = sheets.createSheet("sheet第一个");
        HSSFRow row1 = sheet.createRow(0);
        row1.createCell(0).setCellValue("标题1");
        row1.createCell(1).setCellValue("标题2");
        row1.createCell(2).setCellValue("标题3");
        row1.createCell(3).setCellValue("标题4");
        row1.createCell(4).setCellValue("标题5");
        row1.createCell(5).setCellValue("标题6");
        row1.createCell(6).setCellValue("标题7");
        row1.createCell(7).setCellValue("标题8");
        row1.createCell(8).setCellValue("标题9");
        row1.createCell(9).setCellValue("标题10");

        for (int i = 1; i < 65536; i++) {
            HSSFRow row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                row.createCell(j).setCellValue("aaaaaaa");
            }
        }
        long l2 = System.currentTimeMillis();
        System.out.println("用时："+(l2-l1));
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        sheets.write(fileOutputStream);
        fileOutputStream.close();
    }


    public static void write07(String file) throws IOException{
        XSSFWorkbook sheets = new XSSFWorkbook();
        Sheet sheet = sheets.createSheet("这是07版第一个sheet");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("标题");
        row.createCell(1).setCellValue("age");
        row.createCell(2).setCellValue("name");

        Row row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue("标题1");
        row1.createCell(1).setCellValue(20);
        row1.createCell(2).setCellValue("weifeng");

        Row row2 = sheet.createRow(2);
        row1.createCell(0).setCellValue("标题1");
        row1.createCell(1).setCellValue(20);
        row1.createCell(2).setCellValue("weifeng");



        FileOutputStream fileOutputStream = new FileOutputStream(file);
        sheets.write(fileOutputStream);
        fileOutputStream.close();
    }

    public static void write07_gognshi(String file) throws IOException{
        XSSFWorkbook sheets = new XSSFWorkbook();
        Sheet sheet = sheets.createSheet("这是07版第一个sheet");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("标题");
        row.createCell(1).setCellValue("age");
        row.createCell(2).setCellValue("name");

        Row row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue("标题1");
        row1.createCell(1).setCellValue(20);
        row1.createCell(2).setCellValue("weifeng");

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        sheets.write(fileOutputStream);
        fileOutputStream.close();
    }

}
